// @charset "utf-8";
/**
 * interactions
 */
var interactions = {

	originalDateStr : '',
	datePicker : async function (date, id, callback, callbacks) {
		var self = this;
		var $datePicker = $('#view_input_' + id + 'date');

		$datePicker.val(commonMethod.dateSplit(date));
		self.originalDateStr = commonMethod.dateSplit(date);

		var elstart,
			elend,
			minstart,
			minend,
			maxstart;
		var statId = '';
		if (id == 'zqxy') {
			elstart = 'view_input_zqxydate_start';
			elend = 'view_input_zqxydate_end';
			minstart = '2016-03-28';
			maxstart = '#F{$dp.$D(\'view_input_zqxydate_end\', {d: -1})}';
			minend = '#F{$dp.$D(\'view_input_zqxydate_start\',{d: +1})}';
			statId = 'cop_56d3b197_545';
		} else if (id == 'qrpj') {
			elstart = 'view_input_qrpjdate_start';
			elend = 'view_input_qrpjdate_end';
			minstart = '2016-01-04';
			maxstart = '#F{$dp.$D(\'view_input_qrpjdate_end\', {d: -1})}';
			minend = '#F{$dp.$D(\'view_input_qrpjdate_start\',{d: +1})}';
			statId = 'cop_56d3b0f7_159';
		} else if (id == 'dqfx') {
			elstart = 'view_input_dqfxdate_start';
			elend = 'view_input_dqfxdate_end';
			minstart = '2016-01-04';
			maxstart = '#F{$dp.$D(\'view_input_dqfxdate_end\', {d: -1})}';
			minend = '#F{$dp.$D(\'view_input_dqfxdate_start\',{d: +1})}';
			statId = 'cop_573ed839_854';
		} else if (id == 'ggzd') {
			minstart = '2016-01-04';
			statId = 'cop_56d3b1da_644';
		}

		$datePicker.on('click', async function() {
			WdatePicker({
				skin: 'freehomepage',
				readOnly: true,
				isShowClear: false,
				isShowToday: true,
				isShowOK: true,
				qsEnabled: false,
				dateFmt: 'yyyy-MM-dd',
				minDate: minstart,
				maxDate: '%y-%M-%d',
				disabledDays: [0, 6],
				onpicked: async function(dp) {
					var today = await commonMethod.serverTime();
					var newdate = parseInt((dp.cal.getNewDateStr()).replace(/-/g, ''));
					var tradingDay = await commonMethod.thsQuote.getSHTradeDate(newdate);
					if (tradingDay != newdate && newdate != today) {
						$datePicker.val(self.originalDateStr);
						$('.view_p_nontrading').fadeIn(100);
						setTimeout(function () {
							$('.view_p_nontrading').fadeOut(800);
						}, 1200);
						$datePicker.blur();
						return;
					} else {
						self.originalDateStr = dp.cal.getNewDateStr();
					}
					$datePicker.blur();
					callback(newdate);
					TA.log(statId);
				}
			});
		});
		// 区间
		var $startDom = $('#view_input_'+ id +'date_start');
		var $endDom = $('#view_input_'+ id +'date_end');

		var startDate = undefined,
			endDate = undefined;
		var tradingDay = await commonMethod.getTradingDay();
		var preMonthDate = await commonMethod.preMonthDate();
		if (id === 'dqfx') {
			startDate = preMonthDate.replace(/-/g,'');
			endDate = tradingDay;
			$startDom.val(preMonthDate);
			$endDom.val(commonMethod.dateSplit(tradingDay));
			// callbacks(startDate, endDate);
		}

		$('.interval-btn').on('click', function () {
			$(this).addClass('hide');
			$(this).parent('.wrap-date-single').hide();
			$(this).parent('.wrap-date-single').next('.wrap-date-interval').show();

			$('.reading-footer').hide();
			startDate = preMonthDate.replace(/-/g,'');
			endDate = tradingDay;

			$startDom.val(preMonthDate);
			$endDom.val(commonMethod.dateSplit(tradingDay));
			callbacks(startDate, endDate);
		});

		$('.backdate-btn').on('click', function () {
			$(this).parent('.wrap-date-interval').hide();
			$(this).parent('.wrap-date-interval').prev('.wrap-date-single').show();
			$('.reading-footer').show();
			$datePicker.val(commonMethod.dateSplit(tradingDay));

			callback(tradingDay);
		});

		$startDom.on('click', function () {
			WdatePicker({
				skin: 'freehomepage',
				el : elstart,
				readOnly: true,
				isShowClear: false,
				isShowToday: true,
				isShowOK: true,
				qsEnabled: false,
				dateFmt: 'yyyy-MM-dd',
				minDate: minstart,
				maxDate: maxstart,
				disabledDays: [0, 6],
				onpicked: function(dp) {
					startDate = parseInt((dp.cal.getNewDateStr()).replace(/-/g, ''));
					endDate = endDate === undefined ? parseInt($endDom.val().replace(/-/g,'')) : endDate;
					$startDom.blur();
					callbacks(startDate, endDate);
					TA.log(statId);
				}
			});
		});

		$endDom.on('click', function () {
			WdatePicker({
				skin: 'freehomepage',
				el : elend,
				readOnly: true,
				isShowClear: false,
				isShowToday: true,
				isShowOK: true,
				qsEnabled: false,
				dateFmt: 'yyyy-MM-dd',
				minDate: minend,
				maxDate: '%y-%M-%d',
				disabledDays: [0, 6],
				onpicked: function(dp) {
					endDate = parseInt((dp.cal.getNewDateStr()).replace(/-/g, ''));
					startDate = startDate === undefined ? parseInt($startDom.val().replace(/-/g,'')) : startDate;
					$endDom.blur();
					callbacks(startDate, endDate);
					TA.log(statId);
				}
			});
		});
	},
	ajaxJson : function (paramobj) {
		var defaultparam = {
			type : 'GET',
			url : '',
			data : {},
			cache : false,
			callback : ''
		};
		var param = $.extend(defaultparam, paramobj);
		$.ajax({
			type: param.type,
			url: param.url,
			data: param.data,
			cache: param.cache,
			dataType: 'json',
			success: function(result) {
				param.callback(result);
			},
			error: function(e) {
				param.callback({});
			}
		});
	},
	ajaxJsonp : function (paramobj) {
		var defaultparam = {
			type : 'GET',
			url : '',
			data : {},
			cache : false,
			jsonpCallback : '',
			callback : ''
		};
		var param = $.extend(defaultparam, paramobj);
		$.ajax({
			type: param.type,
			url: param.url,
			data: param.data,
			cache: param.cache,
			dataType: 'jsonp',
			jsonp: false,
			jsonpCallback: param.jsonpCallback,
			success: function(result) {
				// alert(JSON.stringify(result));
				param.callback(result);
			},
			error: function(e) {
				// alert('e');
				param.callback({});
			}
		});
	},
	switchTab : function (callback) {
		$('.tab-link').on('click', function () {
			if (!$(this).hasClass('tab-active')) {
				$(this).addClass('tab-active').siblings().removeClass('tab-active');
				var index = $(this).index();
				$('.tab-content > .tab-content-item').hide().eq(index).show();
				$('.tab-date-box > .tab-date-item').hide().eq(index).show();

				if (callback) {
					callback(index);
				}
			}
		});
	},
	switchDefTab : function (index) {
		$('.tab-link').eq(index).addClass('tab-active').siblings().removeClass('tab-active');
		$('.tab-content > .tab-content-item').hide().eq(index).show();
		$('.tab-date-box > .tab-date-item').hide().eq(index).show();
	},
	commonEvents : function () {
		$('.drop-interval-btn').on('click', function(e) {
			e = event || window.event;
			if (e && e.stopPropagation) {
				e.stopPropagation();
			} else {
				e.cancelBubble = true;
			}
			$(this).parent('.wrap-date').find('.interval-btn').toggleClass('hide');
		});
		$('body').not('.interval-btn').not('.drop-interval-btn').on('click', function() {
			if ($('.interval-btn').is(':visible')) {
				$('.interval-btn').addClass('hide');
			}
		});

		$('#view_a_readingbtn').on('click', function () {
			if (!$(this).hasClass('cannot-read')) {
				$('.view_p_readingtext').attr('data-isshow', 'no');
				$('.reading-content').toggleClass('hide');
			}
		});

		$('.reading-close').on('click', function () {
			$('.reading-content').addClass('hide');
		});
		$('.tab-content-item:first, .reading-content').on('dblclick', function () {
			$('.view_p_readingtext').attr('data-isshow', 'no');
			$('#view_a_readingbtn').trigger('click');
		});

		$('p[id$="pjhc"]').live('click', function () {
			var url = $(this).find('a').attr('href');
			commonMethod.openUrlWeb(url, 4);
			return false;
		});

		// 移入回测数据
		var inLink = false;
		var inContent = false;
		$('.view_p_readingtext').hover(function() {
			var self = this;
			if (!$(this).next('.reading-btn').hasClass('cannot-read')) {
				var $content = $(this).parents('.container').find('.reading-content');
				inLink = true;
				setTimeout(function() {
					if (inLink || inContent) {
						$('.view_p_readingtext').attr('data-isshow', 'yes');
						$content.removeClass('hide');

						if ($(self).parents('.container').hasClass('zqxy')) {
							TA.log('cop_572ae769_933');
						}
						if ($(self).parents('.container').hasClass('qrpj')) {
							TA.log('cop_570c94c9_516');
						}
						if ($(self).parents('.container').hasClass('ggzd')) {
							TA.log('cop_570c94f5_880');
						}
					}
				}, 300);
			}
		}, function() {
			inLink = false;
			var $content = $(this).parents('.container').find('.reading-content');
			setTimeout(function() {
				if (!inLink && !inContent) {
					$content.addClass('hide');
					$('.view_p_readingtext').attr('data-isshow', 'no');
				} else {

				}
			}, 300);
		});
		$('.reading-content').hover(function() {
			inContent = true;
			setTimeout(function() {
				if (inLink || inContent) {
					$('.reading-content').removeClass('hide');
				}
			}, 300);
		}, function() {
			inContent = false;
			$readingText = $(this).parents('.container').find('.view_p_readingtext');
			setTimeout(function() {
				if (!inLink && !inContent) {
					if ($readingText.attr('data-isshow') == 'yes') {
						$('.reading-content').addClass('hide');
					}
				}
			}, 300);
		});

		// 一键申购
		$('.view_a_apply').live('click', function () {
			commonMethod.thsUtil.setNewWeituoBtnClicked("button_clicked=41");
		});

		$('[data-stat]').live('click', function () {
			var statId = $(this).attr('data-stat');
			TA.log(statId);
		});
	},
	requestZxData : function(type, startDate) {
		var self = this;
		var url = 'zx/get_homepage_data.lua?op=' + type + '&begin_date=' + startDate + '&end_date=' + startDate;
		var bool = commonMethod.thsQuote.requestZxData({
			url: url,
			onready: function(res) {
				if (res == '[]') {
					self.renderRisingProb(type, 'NUL');
				} else {
					res = JSON.parse(res);
					self.renderRisingProb(type, res[0]['data']);
				}

			}
		});
		if (!bool) {
			self.renderRisingProb(type, 'NUL');
		}
	},
	renderRisingProb : function (type, res) {
		var $risingProb = $('#view_span_'+ type),
			$risingHcInfo = $('#view_p_'+ type);
		if (res == 'NUL') {
			$risingProb.text('次日大盘上涨概率--%');
			$risingHcInfo.text('历史数据回测中...');
		} else {
			$risingProb.html(res.info);
			var hcContent = "<a href = '" + res.url + "'>" + res.detail +"</a>";
			$risingHcInfo.html(hcContent);
		}
	},
	autoLoop : function (callback, delay) {
		// var self = this;
		setInterval(function () {
			callback();
		}, delay);
		// setTimeout(function () {
		// 	self.autoLoop();
		// }, 5000);
	}
};